package org.zfes.snowier.cloud.zbss.metting.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.metting.biz.service.ISignupModelItemService;
import org.zfes.snowier.cloud.zbss.metting.biz.dao.SignupModelItemMapper;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.SignupModelItemUpdateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.model.SignupModelItem;
import java.util.stream.Stream;
@Service
public class SignupModelItemServiceImpl implements ISignupModelItemService{
 	@Autowired
	private SignupModelItemMapper signupModelItemMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createSignupModelItem(SignupModelItem signupModelItem) {
		ZBeanUtil.setCreateInfo(signupModelItem);
		ZBeanUtil.validateBean(signupModelItem);
		signupModelItemMapper.insertSelective(signupModelItem);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<SignupModelItem> loadSignupModelItemById(Long id) {
		if(id!=null){
			return Optional.ofNullable(signupModelItemMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SignupModelItem> loadItemsByModelId(Long modelId){
		if(modelId==null) {
			return Lists.newArrayList();
		}
		return signupModelItemMapper.selectByModelId(modelId);
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateSignupModelItem(SignupModelItemUpdateDto modelItemUpdateDto) {
		ZBeanUtil.setUpdatedInfo(modelItemUpdateDto);
		ZBeanUtil.validateBean(modelItemUpdateDto);
		
		SignupModelItem item=loadSignupModelItemById(modelItemUpdateDto.getId()).orElseThrow(()->ZAlert.newSLE("未查询到数据"));
		ZBeanUtil.copy(modelItemUpdateDto, item, true);
		signupModelItemMapper.updateByPrimaryKeySelective(item);
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setSignupModelItemStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		signupModelItemMapper.updateStatus( id,  status) ;
	}
	
}